package com.lonely.fx.table_view;

import com.lonely.fx.table_view.entity.SimplePerson;
import javafx.application.Application;
import javafx.beans.binding.DoubleBinding;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

/**
 * @date: 2022-08-02 11:03
 * @author: ztkj-hzb
 * @description: 合并单元格属性设置
 **/
public class MergeCellSettingMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {


        AnchorPane anchorPane = new AnchorPane();

        //创建一个基于简单的javabean的tableview
        TableView<SimplePerson> tableView = new TableView<>();

        //设置宽高
        tableView.setPrefSize(600, 400);

        //设置数据
        ObservableList<SimplePerson> persons = FXCollections.observableArrayList();

        persons.add(new SimplePerson("小天", 20, "湖北省", false));
        persons.add(new SimplePerson("小地", 30, "湖南省", true));
        persons.add(new SimplePerson("小玄", 40, "广东省", false));
        persons.add(new SimplePerson("小黄", 50, "浙江省", true));
        tableView.setItems(persons);

        //创建列
        TableColumn<SimplePerson, Object> personTableCol = new TableColumn<>("用户基础信息");
        TableColumn<SimplePerson, String> nameTableCol = new TableColumn<>("用户名");
        TableColumn<SimplePerson, Number> ageTableCol = new TableColumn<>("年龄");
        TableColumn<SimplePerson, String> addressTableCol = new TableColumn<>("地址");
        TableColumn<SimplePerson, Boolean> marriedTableCol = new TableColumn<>("是否已婚");

        //TODO --- 合并单元格
        personTableCol.getColumns().add(nameTableCol);
        personTableCol.getColumns().add(ageTableCol);

        tableView.getColumns().addAll(personTableCol,addressTableCol, marriedTableCol);

        //设置列与数据绑定
        nameTableCol.setCellValueFactory(new PropertyValueFactory<>("name"));
        addressTableCol.setCellValueFactory(new PropertyValueFactory<>("address"));
        ageTableCol.setCellValueFactory(new PropertyValueFactory<>("age"));
        marriedTableCol.setCellValueFactory(new PropertyValueFactory<>("married"));

        AnchorPane.setTopAnchor(tableView, 100.0);
        AnchorPane.setLeftAnchor(tableView, 100.0);
        anchorPane.getChildren().addAll(tableView);
        primaryStage.setScene(new Scene(anchorPane));
        primaryStage.setWidth(800);
        primaryStage.setHeight(600);
        primaryStage.show();


        //设置tableview中的宽度均分
        System.out.println(tableView.getItems().size());
        DoubleBinding itemWidthBinding = tableView.widthProperty().divide(tableView.getColumns().size());
        for (TableColumn<SimplePerson, ?> column : tableView.getColumns()) {
            column.prefWidthProperty().bind(itemWidthBinding.subtract(1));
        }


    }
}
